package com.tuo51.observation.controller;


import com.tuo51.core.domain.org.Classes;
import com.tuo51.core.domain.org.Organization;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.core.service.ClassesService;
import com.tuo51.core.service.StaffService;
import com.tuo51.evaluate.domain.childrens.Children;
import com.tuo51.evaluate.service.ChildrenService;
import com.tuo51.evaluate.vo.children.ChildrenPO;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 测评小程序使用的接口
 */

@RestController
@RequestMapping("/oapi/children")
@Api(value = "/oapi/children", tags = "观测点 - 宝宝")
public class OBChildrenController {

    @Autowired
    private StaffService staffService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private ChildrenService childrenService;

    @Autowired
    private ClassesService classesService;

    @RequestMapping(value = "/mychildren", method = POST)
    public StatusResponse getChildren(@RequestHeader(TICKET) String ticket){
        Staff staff = cache.getStaff(ticket);

        Object ret = childrenService.getAllWithSourceByParent(staff);

        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/detail", method = POST)
    public StatusResponse childrenDetail(@RequestHeader(TICKET) String ticket, String childrenId){
        String staffId = cache.getStaffId(ticket);

        Children c = childrenService.getById(childrenId);
        if(c == null){
            throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "宝宝不存在");
        }

        c.setMonth(DateUtil.betweenMonths(new Date(), c.getBirthday()));

        return StatusResponse.success(c);
    }

    @RequestMapping(value = "/delete", method = POST)
    public StatusResponse deleteChildren(@RequestHeader(TICKET) String ticket, String childrenId) {
        String staffId = cache.getStaffId(ticket);

        boolean ret = childrenService.deleteChildrenForStaff(childrenId, staffId);

        return StatusResponse.success(ret);
    }

    @ApiOperation("创建或更新宝宝信息，所有字段可选")
    @RequestMapping(value = "/save_children", method = POST)
    public StatusResponse updateChildInfo(@RequestHeader(TICKET) String ticket,@Validated ChildrenPO po) {

        Staff staff = cache.getStaff(ticket);

        Organization org = staffService.staffOrgnization(staff);

        Children children = childrenService.createOrEdit(staff, po, org);

        if (StringUtils.isNotEmpty(po.getClassId())){//处理班级关系
            Classes classes = classesService.getClassByStaff(staff.getId(), po.getClassId());

            classesService.addMemberToClassesId(staff.getId(), children.getId(), classes.getId());
        }

        return StatusResponse.success(children);
    }
}
